asm
  ld sp,$8FFF
  end asm

#include "./library/smsrnd.bas"
#include "./library/smsdelay.bas"

dim seed as uinteger at $8810
dim ee1 as uinteger at $8812
dim ee2 as uinteger at $8814
dim ee3 as uinteger at $8816
dim ee4 as uinteger at $8818
dim cnt as uinteger at $881A
dim ex1 as uinteger at $881C
dim ey1 as uinteger at $881E


sub pooyanputchar(tx1 as uinteger, ty1 as uinteger, tch1 as ubyte, tat1 as ubyte)
  poke ($8400+(tx1*32)+ty1) bxor %1111100000,tch1
  poke ($8000+(tx1*32)+ty1) bxor %1111100000,tat1
  end sub

sub pooyanwritetext(tx2 as uinteger,ty2 as uinteger,tad2 as uinteger,tl2 as uinteger,tat2 as ubyte)
  dim tz2 as uinteger at $8800
  for tz2=0 to tl2-1
    pooyanputchar(tx2+tz2,ty2,peek(tad2+tz2),tat2)
    next
  end sub

sub pooyanputsprite(tl1 as uinteger,tx1 as ubyte, ty1 as ubyte, tch1 as ubyte, tat1 as ubyte)
  poke $9000+(tl1*2),ty1      '- inv y
  poke $9001+(tl1*2),tch1     '- id?
  poke $9400+(tl1*2),tat1     '- attr?
  poke $9401+(tl1*2),tx1      '- x
  end sub

'sub c1942putchar(tx1 as uinteger, ty1 as uinteger, tch1 as ubyte, tat1 as ubyte)
'  poke ($D000+(tx1*32)+ty1) bxor 31,tch1  '- video ram
'  poke ($D400+(tx1*32)+ty1) bxor 31,tat1  '- color ram
'  end sub

'sub c1942puttile(tx1 as uinteger, ty1 as uinteger, tch1 as ubyte, tat1 as ubyte)
'  poke ($D800+(tx1*32)+ty1) bxor %1111100000,tch1
'  poke ($D800+(tx1*32)+ty1+16) bxor %1111100000,tat1
'  end sub



poke $A180,1
poke $A180,0
poke $A180,255

for ee1=0 to 31
  for ee2=0 to 31
    seed=smsrnd(seed):ee4=seed
    seed=smsrnd(seed)
    pooyanputchar(ee2,ee1,ee4 band 3,seed)
    'smsdelay(100)
    next:next

for ee1=0 to 15
  for ee2=0 to 15
    pooyanputchar(ee2+3,ee1+1,ee1*16+ee2,0)
    'smsdelay(100)
    next:next

pooyanwritetext(12,18,@text01,@text01end-@text01,1)

for ee1=$10 to $3F
  seed=smsrnd(seed)
  poke $9000+ee1,seed
  seed=smsrnd(seed)
  poke $9400+ee1,seed

  pooyanputchar(8,0,48+(ee1 mod 10),0)
  pooyanputchar(7,0,48+((int(ee1/10)) mod 10),0)
  pooyanputchar(6,0,48+((int(ee1/100)) mod 10),0)

  'smsdelay(40000)
  next

ex1=128:ey1=160
cnt=0
do
  ee3=peek($A0A0) bxor 255
  pooyanputchar(5,18,48+(ee3 mod 10),0)
  pooyanputchar(4,18,48+((int(ee3/10)) mod 10),0)
  pooyanputchar(3,18,48+((int(ee3/100)) mod 10),0)
  ee4=peek($A0C0) bxor 255
  pooyanputchar(5,19,48+(ee4 mod 10),0)
  pooyanputchar(4,19,48+((int(ee4/10)) mod 10),0)
  pooyanputchar(3,19,48+((int(ee4/100)) mod 10),0)

  ey1=ey1-((ee3 band 4)/4)+((ee3 band 8)/8)
  ex1=ex1-((ee4 band 4)/4)+((ee4 band 8)/8)

  pooyanputsprite(16,ex1,ey1,cnt,cnt)

  smsdelay(500)
  cnt=cnt+1
  loop




do
  'poke $a000,0
  'poke $a000,255
  'poke $5e7f,0
  'poke $5e7f,255

  for ee1=$00 to $FF
    ee4=peek($9000+ee1)
    for ee2=0 to 255
      poke $9000+ee1,ee2
      smsdelay(500)
      next
    poke $9000+ee1,ee4
    ee4=peek($9400+ee1)
    for ee2=0 to 255
      poke $9400+ee1,ee2
      smsdelay(500)
      next
    poke $9400+ee1,ee4
    next
  loop

do
  loop


text01:
asm
  defb "HELLO WORLD!!!!!"
  end asm
text01end:


' AM_RANGE(0x0000, 0x7fff) AM_ROM
' AM_RANGE(0x8000, 0x83ff) AM_RAM_WRITE(pooyan_colorram_w) AM_SHARE("colorram")
' AM_RANGE(0x8400, 0x87ff) AM_RAM_WRITE(pooyan_videoram_w) AM_SHARE("videoram")
' AM_RANGE(0x8800, 0x8fff) AM_RAM
' AM_RANGE(0x9000, 0x90ff) AM_MIRROR(0x0b00) AM_RAM AM_SHARE("spriteram")
' AM_RANGE(0x9400, 0x94ff) AM_MIRROR(0x0b00) AM_RAM AM_SHARE("spriteram2")
' AM_RANGE(0xa000, 0xa000) AM_MIRROR(0x5e7f) AM_READ_PORT("DSW1")
' AM_RANGE(0xa080, 0xa080) AM_MIRROR(0x5e1f) AM_READ_PORT("IN0")
' AM_RANGE(0xa0a0, 0xa0a0) AM_MIRROR(0x5e1f) AM_READ_PORT("IN1")
' AM_RANGE(0xa0c0, 0xa0c0) AM_MIRROR(0x5e1f) AM_READ_PORT("IN2")
' AM_RANGE(0xa0e0, 0xa0e0) AM_MIRROR(0x5e1f) AM_READ_PORT("DSW0")
' AM_RANGE(0xa000, 0xa000) AM_MIRROR(0x5e7f) AM_WRITE(watchdog_reset_w)
' AM_RANGE(0xa100, 0xa100) AM_MIRROR(0x5e7f) AM_WRITE(soundlatch_byte_w)
' AM_RANGE(0xa180, 0xa180) AM_MIRROR(0x5e78) AM_WRITE(irq_enable_w)
' AM_RANGE(0xa181, 0xa181) AM_MIRROR(0x5e78) AM_WRITE_LEGACY(timeplt_sh_irqtrigger_w)
' AM_RANGE(0xa183, 0xa183) AM_MIRROR(0x5e78) AM_WRITENOP // ???
' AM_RANGE(0xa187, 0xa187) AM_MIRROR(0x5e78) AM_WRITE(pooyan_flipscreen_w)

'-------------------------------------------------------------
'dim ee2 as uinteger at $4C06
'dim ee3 as uinteger at $4C08
'dim ee4 as ubyte at $4C0A
'dim ecsp as ubyte at $4C0C
'dim ecsq as ubyte at $4C0E
'
'dim ex0 as uinteger at $4C10
'dim ey0 as uinteger at $4C12
'dim ex1 as uinteger at $4C14
'dim ey1 as uinteger at $4C16
'dim ex2 as uinteger at $4C18
'dim ey2 as uinteger at $4C1A
'dim ex3 as uinteger at $4C1C
'dim ey3 as uinteger at $4C1E
'
'dim ex4 as uinteger at $4C20
'dim ey4 as uinteger at $4C22
'dim ex5 as uinteger at $4C24
'dim ey5 as uinteger at $4C26
'dim ex6 as uinteger at $4C28
'dim ey6 as uinteger at $4C2A
'dim ex7 as uinteger at $4C2C
'dim ey7 as uinteger at $4C2E
'
'dim ej0 as uinteger at $4C30
'dim ej1 as uinteger at $4C32
'
'function test1(t1 as uinteger,t2 as uinteger,t3 as uinteger,t4 as uinteger)  as uinteger
'  end function
'
'sub zpoke(t1 as uinteger,t2 as ubyte)
'  poke t1,t2
'  end sub
'
'sub putchar(tx1 as uinteger, ty1 as uinteger, tch1 as uinteger)
'  poke ($4000+(tx1*32)+ty1) bxor %0000001111100000,(tch1 band 255)
'  end sub
'
'pacmanputsprite(0,21,10,4,3)
'pacmanputsprite(1,21,30,4,3)
'pacmanputsprite(2,21,50,4,3)
'pacmanputsprite(3,21,70,4,3)
'pacmanputsprite(4,21,90,4,3)
'pacmanputsprite(5,21,110,4,3)
'pacmanputsprite(6,21,130,4,3)
'pacmanputsprite(7,21,150,4,3)
'
'seed=0
'
'poke $4300,code("*")
'poke $4200,code("*")
'poke $4100,code("*")
'
'poke $4010,code("0")
'poke $400F,code("1")
'poke $4030,code("2")
'poke $402F,code("3")
'
'poke $43D0,code("0")
'poke $43CF,code("1")
'poke $43F0,code("2")
'poke $43EF,code("3")
'
'for ee2=0 to 11
'  poke ($4000+((ee2+12)*32)+13) bxor %0000001111100000,peek(@text01+ee2)
'  next
'
'for ee2=0 to 11
'  poke ($4000+(0*32)+(ee2+12)) bxor %0000000000011111,peek(@text01+ee2)+64
'  next
'
'for ee2=0 to 11
'  poke ($4000+(31*32)+(ee2+12)) bxor %0000000000011111,peek(@text01+ee2)+128
'  next
'
'ee4=0
'for ee3=15 to 15+15
'  for ee2=12 to 12+15
'    poke ($4000+(ee2*32)+ee3) bxor %0000001111100000,ee4
'    ee4=ee4+1
'    next
'    next
'
'for ee2=22 to 25
'  for ee3=6 to 9
'    poke ($4000+(ee2*32)+ee3) bxor %0000001111100000,code("X")
'    next
'    next
'
'test1(377,2445,63888,2342)
'
'ee1=$4207
'i=code("Q")
'poke ee1,i
'ee1=ee1+1
'i=code("T")
'poke ee1,i
'
'seed=0
'for ee1=$4400 to $47FF
'  seed=smsrnd(seed)
'  poke ee1,seed
'  next
'
'ex0=20:ey0=10
'ex1=20:ey1=30
'ex2=20:ey2=50
'ex3=20:ey3=70
'ex4=20:ey4=90
'ex5=20:ey5=110
'ex6=20:ey6=130
'ex7=20:ey7=150
'ee1=0
'ecsp=0:ecsq=0
'
'do
'poke $50C0,255 '- watchdog? - it seems to work! thanks, Jim Bagley!!!!
'
'ej0=peek($5000) bxor $ff
'ej1=peek($5040) bxor $ff
'
'poke ($4000+(6*32)+20) bxor %0000001111100000,ej0
'poke ($4000+(7*32)+20) bxor %0000001111100000,ej1
'
'poke ($4000+(8*32)+22) bxor %0000001111100000,48+(ex0 mod 10)
'poke ($4000+(7*32)+22) bxor %0000001111100000,48+((int(ex0/10)) mod 10)
'poke ($4000+(6*32)+22) bxor %0000001111100000,48+((int(ex0/100)) mod 10)
'
'poke ($4000+(8*32)+23) bxor %0000001111100000,48+(ey0 mod 10)
'poke ($4000+(7*32)+23) bxor %0000001111100000,48+((int(ey0/10)) mod 10)
'poke ($4000+(6*32)+23) bxor %0000001111100000,48+((int(ey0/100)) mod 10)
'
'poke ($4000+(8*32)+24) bxor %0000001111100000,48+(ee1 mod 10)
'poke ($4000+(7*32)+24) bxor %0000001111100000,48+((int(ee1/10)) mod 10)
'poke ($4000+(6*32)+24) bxor %0000001111100000,48+((int(ee1/100)) mod 10)
'poke ($4000+(5*32)+24) bxor %0000001111100000,48+((int(ee1/1000)) mod 10)
'poke ($4000+(4*32)+24) bxor %0000001111100000,48+((int(ee1/10000)) mod 10)
'
'poke ($4000+(8*32)+25) bxor %0000001111100000,48+(ej0 mod 10)
'poke ($4000+(7*32)+25) bxor %0000001111100000,48+((int(ej0/10)) mod 10)
'poke ($4000+(6*32)+25) bxor %0000001111100000,48+((int(ej0/100)) mod 10)
'
'poke ($4000+(8*32)+26) bxor %0000001111100000,48+(ej1 mod 10)
'poke ($4000+(7*32)+26) bxor %0000001111100000,48+((int(ej1/10)) mod 10)
'poke ($4000+(6*32)+26) bxor %0000001111100000,48+((int(ej1/100)) mod 10)
'
'poke ($4000+(8*32)+27) bxor %0000001111100000,48+(ecsp mod 10)
'
'
'if ecsp=0 then:
'  if (ej0 band 1)<>0 then: ey0=ey0-1:end if
'  if (ej0 band 2)<>0 then: ex0=ex0-1:end if
'  if (ej0 band 4)<>0 then: ex0=ex0+1:end if
'  if (ej0 band 8)<>0 then: ey0=ey0+1:end if
'  end if
'
'if ecsp=1 then:
'  if (ej0 band 1)<>0 then: ey1=ey1-1:end if
'  if (ej0 band 2)<>0 then: ex1=ex1-1:end if
'  if (ej0 band 4)<>0 then: ex1=ex1+1:end if
'  if (ej0 band 8)<>0 then: ey1=ey1+1:end if
'  end if
'
'if ecsp=2 then:
'  if (ej0 band 1)<>0 then: ey2=ey2-1:end if
'  if (ej0 band 2)<>0 then: ex2=ex2-1:end if
'  if (ej0 band 4)<>0 then: ex2=ex2+1:end if
'  if (ej0 band 8)<>0 then: ey2=ey2+1:end if
'  end if
'
'if ecsp=3 then:
'  if (ej0 band 1)<>0 then: ey3=ey3-1:end if
'  if (ej0 band 2)<>0 then: ex3=ex3-1:end if
'  if (ej0 band 4)<>0 then: ex3=ex3+1:end if
'  if (ej0 band 8)<>0 then: ey3=ey3+1:end if
'  end if
'
'if ecsp=4 then:
'  if (ej0 band 1)<>0 then: ey4=ey4-1:end if
'  if (ej0 band 2)<>0 then: ex4=ex4-1:end if
'  if (ej0 band 4)<>0 then: ex4=ex4+1:end if
'  if (ej0 band 8)<>0 then: ey4=ey4+1:end if
'  end if
'
'if ecsp=5 then:
'  if (ej0 band 1)<>0 then: ey5=ey5-1:end if
'  if (ej0 band 2)<>0 then: ex5=ex5-1:end if
'  if (ej0 band 4)<>0 then: ex5=ex5+1:end if
'  if (ej0 band 8)<>0 then: ey5=ey5+1:end if
'  end if
'
'if ecsp=6 then:
'  if (ej0 band 1)<>0 then: ey6=ey6-1:end if
'  if (ej0 band 2)<>0 then: ex6=ex6-1:end if
'  if (ej0 band 4)<>0 then: ex6=ex6+1:end if
'  if (ej0 band 8)<>0 then: ey6=ey6+1:end if
'  end if
'
'if ecsp=7 then:
'  if (ej0 band 1)<>0 then: ey7=ey7-1:end if
'  if (ej0 band 2)<>0 then: ex7=ex7-1:end if
'  if (ej0 band 4)<>0 then: ex7=ex7+1:end if
'  if (ej0 band 8)<>0 then: ey7=ey7+1:end if
'  end if
'
'if (ej1 band 15)<>0 then: ecsq=ecsq+1:end if
'ecsp=(int(ecsq/4))mod 8
'
'ee3=int(ee1/4)
'
'pacmanputsprite(0,ex0,ey0,4,3)  '- layer,x,y,id,clut
'pacmanputsprite(1,ex1,ey1,ee3,3)  '- layer,x,y,id,clut
'pacmanputsprite(2,ex2,ey2,4,ee3)  '- layer,x,y,id,clut
'pacmanputsprite(3,ex3,ey3,ee3,ee3)  '- layer,x,y,id,clut
'pacmanputsprite(4,ex4,ey4,4,3)  '- layer,x,y,id,clut
'pacmanputsprite(5,ex5,ey5,ee3,3)  '- layer,x,y,id,clut
'pacmanputsprite(6,ex6,ey6,4,ee3)  '- layer,x,y,id,clut
'pacmanputsprite(7,ex7,ey7,ee3,ee3)  '- layer,x,y,id,clut
'
'ee1=ee1+1
'loop
'
''-------------------------------------------------------------------------------
'
'do
'loop
'
''-------------------------------------------------------------------------------
'
'
''-------------------------------------------------------------------------------
'
'poke $5000,255
'
''- this should put some characters on the pattern display area
'for i=0 to 255
'  poke $4040+i,i
'  poke $4440+i,i
'  next
'
'do
'loop
'
''-------------------------------------------------------------------------------
'
'
'
